﻿using System.Text.RegularExpressions;

namespace ICommonLibrary.Resource {

    public class AssemblyResourceLookup : IResourceLookup {

        public string Description { get; private set; }
        public string AssemblyString { get; set; }
        public string PathPrefix { get; set; }

        private static readonly Regex SlashPattern = new Regex(@"\\+");

        public AssemblyResourceLookup(string assemblyString) : this(assemblyString, ".") {
        }

        public AssemblyResourceLookup(string assemblyString, string pathPrefix) {
            AssemblyString = assemblyString;
            PathPrefix = Util.GetString(pathPrefix);
            if (!PathPrefix.EndsWith(".")) {
                PathPrefix += ".";
            }
            Description = string.Format("AssemblyResourceLookup[{0}, {1}]", AssemblyString, PathPrefix);
        }

        public IResource Lookup(string path) {
            string resourcePath = PathPrefix + path;
            resourcePath = SlashPattern.Replace(resourcePath, ".");
            return new AssemblyResource(AssemblyString, resourcePath);
        }

    }
}